home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / VKAFKE (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  12.8 KB  |  355 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.UIManager;
  5. import com.sun.java.swing.border.Border;
  6. import com.sun.java.swing.plaf.TextUI;
  7. import com.sun.java.swing.plaf.UIResource;
  8. import com.sun.java.swing.text.BadLocationException;
  9. import com.sun.java.swing.text.Caret;
  10. import com.sun.java.swing.text.DefaultCaret;
  11. import com.sun.java.swing.text.DefaultEditorKit;
  12. import com.sun.java.swing.text.DefaultHighlighter;
  13. import com.sun.java.swing.text.Document;
  14. import com.sun.java.swing.text.EditorKit;
  15. import com.sun.java.swing.text.Element;
  16. import com.sun.java.swing.text.Highlighter;
  17. import com.sun.java.swing.text.JTextComponent;
  18. import com.sun.java.swing.text.Keymap;
  19. import com.sun.java.swing.text.View;
  20. import com.sun.java.swing.text.ViewFactory;
  21. import java.awt.Color;
  22. import java.awt.Component;
  23. import java.awt.Container;
  24. import java.awt.Dimension;
  25. import java.awt.Font;
  26. import java.awt.Graphics;
  27. import java.awt.Insets;
  28. import java.awt.Point;
  29. import java.awt.Rectangle;
  30. import java.awt.Shape;
  31. import java.beans.PropertyChangeEvent;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.Serializable;
  35.  
  36. public abstract class BasicTextUI extends TextUI implements ViewFactory, Serializable {
  37.    private static final EditorKit defaultKit = new DefaultEditorKit();
  38.    transient JTextComponent editor;
  39.    transient boolean painted = false;
  40.    transient RootView rootView = new RootView(this);
  41.    transient UpdateHandler updateHandler = new UpdateHandler(this);
  42.  
  43.    public View create(Element elem) {
  44.       return null;
  45.    }
  46.  
  47.    public View create(Element elem, int p0, int p1) {
  48.       return null;
  49.    }
  50.  
  51.    protected Caret createCaret() {
  52.       return new DefaultCaret();
  53.    }
  54.  
  55.    protected Highlighter createHighlighter() {
  56.       return new DefaultHighlighter();
  57.    }
  58.  
  59.    protected Keymap createKeymap() {
  60.       String nm = this.getKeymapName();
  61.       Keymap map = JTextComponent.getKeymap(nm);
  62.       if (map == null) {
  63.          Keymap parent = JTextComponent.getKeymap("default");
  64.          map = JTextComponent.addKeymap(nm, parent);
  65.          String prefix = this.getPropertyPrefix();
  66.          Object o = UIManager.get(prefix + ".keyBindings");
  67.          if (o != null && o instanceof JTextComponent.KeyBinding[]) {
  68.             JTextComponent.KeyBinding[] bindings = (JTextComponent.KeyBinding[])o;
  69.             JTextComponent.loadKeymap(map, bindings, this.getComponent().getActions());
  70.          }
  71.       }
  72.  
  73.       return map;
  74.    }
  75.  
  76.    public void damageRange(int p0, int p1) {
  77.       if (this.painted) {
  78.          Rectangle alloc = this.getVisibleEditorRect();
  79.  
  80.          try {
  81.             Shape s0 = this.rootView.modelToView(p0, alloc);
  82.             Shape s1 = this.rootView.modelToView(p1, alloc);
  83.             if (s0 != null && s1 != null) {
  84.                Rectangle r0 = s0.getBounds();
  85.                Rectangle r1 = s1.getBounds();
  86.                if (r0.y == r1.y) {
  87.                   this.editor.repaint(r0.x, r0.y, r1.x - r0.x + 1, r0.height);
  88.                } else {
  89.                   this.editor.repaint(alloc.x, r0.y, alloc.width, r1.y - r0.y + r1.height);
  90.                }
  91.             }
  92.          } catch (BadLocationException var8) {
  93.          }
  94.       }
  95.  
  96.    }
  97.  
  98.    protected final JTextComponent getComponent() {
  99.       return this.editor;
  100.    }
  101.  
  102.    public Insets getDefaultMargin() {
  103.       return new Insets(0, 0, 0, 0);
  104.    }
  105.  
  106.    public EditorKit getEditorKit() {
  107.       return defaultKit;
  108.    }
  109.  
  110.    protected String getKeymapName() {
  111.       String nm = this.getClass().getName();
  112.       int index = nm.lastIndexOf(46);
  113.       if (index >= 0) {
  114.          nm = nm.substring(index + 1, nm.length());
  115.       }
  116.  
  117.       return nm;
  118.    }
  119.  
  120.    public Dimension getMaximumSize(JComponent c) {
  121.       Insets i = c.getInsets();
  122.       long width = this.rootView.getResizeWeight(0) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
  123.       long height = this.rootView.getResizeWeight(1) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
  124.       return new Dimension((int)width, (int)height);
  125.    }
  126.  
  127.    public Dimension getMinimumSize(JComponent c) {
  128.       Insets i = c.getInsets();
  129.       long width = this.rootView.getResizeWeight(0) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
  130.       long height = this.rootView.getResizeWeight(1) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
  131.       return new Dimension((int)width, (int)height);
  132.    }
  133.  
  134.    public Dimension getPreferredSize(JComponent c) {
  135.       Insets i = c.getInsets();
  136.       Dimension d = ((Component)c).getSize();
  137.       if (d.width > i.left + i.right && d.height > i.top + i.bottom) {
  138.          this.rootView.setSize((float)(d.width - i.left - i.right), (float)(d.height - i.top - i.bottom));
  139.       }
  140.  
  141.       d.width = (int)Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
  142.       d.height = (int)Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
  143.       return d;
  144.    }
  145.  
  146.    protected abstract String getPropertyPrefix();
  147.  
  148.    public View getRootView() {
  149.       return this.rootView;
  150.    }
  151.  
  152.    protected Rectangle getVisibleEditorRect() {
  153.       Rectangle alloc = new Rectangle(this.editor.getSize());
  154.       Insets insets = this.editor.getInsets();
  155.       alloc.x += insets.left;
  156.       alloc.y += insets.top;
  157.       alloc.width -= insets.left + insets.right;
  158.       alloc.height -= insets.top + insets.bottom;
  159.       return alloc;
  160.    }
  161.  
  162.    protected void installDefaults(JComponent c) {
  163.       String prefix = this.getPropertyPrefix();
  164.       Font f = this.editor.getFont();
  165.       if (f == null || f instanceof UIResource) {
  166.          this.editor.setFont(UIManager.getFont(prefix + ".font"));
  167.       }
  168.  
  169.       Color bg = this.editor.getBackground();
  170.       if (bg == null || bg instanceof UIResource) {
  171.          this.editor.setBackground(UIManager.getColor(prefix + ".background"));
  172.       }
  173.  
  174.       Color fg = this.editor.getForeground();
  175.       if (fg == null || fg instanceof UIResource) {
  176.          this.editor.setForeground(UIManager.getColor(prefix + ".foreground"));
  177.       }
  178.  
  179.       Color color = this.editor.getCaretColor();
  180.       if (color == null || color instanceof UIResource) {
  181.          this.editor.setCaretColor(UIManager.getColor(prefix + ".caretForeground"));
  182.       }
  183.  
  184.       Color s = this.editor.getSelectionColor();
  185.       if (s == null || s instanceof UIResource) {
  186.          this.editor.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground"));
  187.       }
  188.  
  189.       Color sfg = this.editor.getSelectedTextColor();
  190.       if (sfg == null || sfg instanceof UIResource) {
  191.          this.editor.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground"));
  192.       }
  193.  
  194.       Color dfg = this.editor.getDisabledTextColor();
  195.       if (dfg == null || dfg instanceof UIResource) {
  196.          this.editor.setDisabledTextColor(UIManager.getColor(prefix + ".inactiveForeground"));
  197.       }
  198.  
  199.       Border b = this.editor.getBorder();
  200.       if (b == null || b instanceof UIResource) {
  201.          this.editor.setBorder(UIManager.getBorder(prefix + ".border"));
  202.       }
  203.  
  204.       Caret caret = this.createCaret();
  205.       this.editor.setCaret(caret);
  206.       Object o = UIManager.get(prefix + ".caretBlinkRate");
  207.       if (o != null && o instanceof Integer) {
  208.          Integer rate = (Integer)o;
  209.          caret.setBlinkRate(rate);
  210.       }
  211.  
  212.    }
  213.  
  214.    protected void installListeners(JComponent c) {
  215.    }
  216.  
  217.    public void installUI(JComponent c) {
  218.       if (c instanceof JTextComponent) {
  219.          this.editor = (JTextComponent)c;
  220.          this.installDefaults(c);
  221.          this.editor.setOpaque(true);
  222.          this.editor.setAutoscrolls(true);
  223.          this.editor.setHighlighter(this.createHighlighter());
  224.          this.editor.addPropertyChangeListener(this.updateHandler);
  225.          Document doc = this.editor.getDocument();
  226.          if (doc == null) {
  227.             this.editor.setDocument(this.getEditorKit().createDefaultDocument());
  228.          } else {
  229.             doc.addDocumentListener(this.updateHandler);
  230.             this.modelChanged();
  231.          }
  232.  
  233.          this.installListeners(c);
  234.          this.editor.setKeymap(this.createKeymap());
  235.       } else {
  236.          throw new Error("TextUI needs JTextComponent");
  237.       }
  238.    }
  239.  
  240.    protected void modelChanged() {
  241.       ViewFactory f = this.rootView.getViewFactory();
  242.       Document doc = this.editor.getDocument();
  243.       Element elem = doc.getDefaultRootElement();
  244.       this.setView(f.create(elem));
  245.    }
  246.  
  247.    public Rectangle modelToView(int pos) throws BadLocationException {
  248.       if (this.painted) {
  249.          Rectangle alloc = this.getVisibleEditorRect();
  250.          Shape s = this.rootView.modelToView(pos, alloc);
  251.          return s.getBounds();
  252.       } else {
  253.          return null;
  254.       }
  255.    }
  256.  
  257.    public final void paint(Graphics g, JComponent c) {
  258.       if (this.rootView.getViewCount() > 0 && this.rootView.getView(0) != null) {
  259.          Runnable painter = new SafePainter(this, g);
  260.          Document doc = this.editor.getDocument();
  261.          doc.render(painter);
  262.       }
  263.  
  264.    }
  265.  
  266.    protected void paintBackground(Graphics g) {
  267.       g.setColor(this.editor.getBackground());
  268.       Dimension d = this.editor.getSize();
  269.       g.fillRect(0, 0, d.width, d.height);
  270.    }
  271.  
  272.    protected void paintSafely(Graphics g) {
  273.       this.painted = true;
  274.       Highlighter highlighter = this.editor.getHighlighter();
  275.       Caret caret = this.editor.getCaret();
  276.       if (this.editor.isOpaque()) {
  277.          this.paintBackground(g);
  278.       }
  279.  
  280.       if (highlighter != null) {
  281.          highlighter.paint(g);
  282.       }
  283.  
  284.       Rectangle alloc = this.getVisibleEditorRect();
  285.       this.rootView.paint(g, alloc);
  286.       if (caret != null) {
  287.          caret.paint(g);
  288.       }
  289.  
  290.    }
  291.  
  292.    protected void propertyChange(PropertyChangeEvent evt) {
  293.    }
  294.  
  295.    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  296.       s.defaultReadObject();
  297.       this.rootView = new RootView(this);
  298.       this.updateHandler = new UpdateHandler(this);
  299.    }
  300.  
  301.    protected final void setView(View v) {
  302.       this.rootView.setView(v);
  303.       this.painted = false;
  304.       this.editor.invalidate();
  305.    }
  306.  
  307.    protected void uninstallDefaults(JComponent c) {
  308.       if (this.editor.getCaretColor() instanceof UIResource) {
  309.          this.editor.setCaretColor((Color)null);
  310.       }
  311.  
  312.       if (this.editor.getSelectionColor() instanceof UIResource) {
  313.          this.editor.setSelectionColor((Color)null);
  314.       }
  315.  
  316.       if (this.editor.getDisabledTextColor() instanceof UIResource) {
  317.          this.editor.setDisabledTextColor((Color)null);
  318.       }
  319.  
  320.       if (this.editor.getSelectedTextColor() instanceof UIResource) {
  321.          this.editor.setSelectedTextColor((Color)null);
  322.       }
  323.  
  324.       if (this.editor.getBorder() instanceof UIResource) {
  325.          this.editor.setBorder((Border)null);
  326.       }
  327.  
  328.       this.editor.setCaret((Caret)null);
  329.    }
  330.  
  331.    protected void uninstallListeners(JComponent c) {
  332.    }
  333.  
  334.    public void uninstallUI(JComponent c) {
  335.       this.editor.removePropertyChangeListener(this.updateHandler);
  336.       this.editor.getDocument().removeDocumentListener(this.updateHandler);
  337.       this.painted = false;
  338.       this.uninstallDefaults(c);
  339.       this.editor.setHighlighter((Highlighter)null);
  340.       this.rootView.setView((View)null);
  341.       ((Container)c).removeAll();
  342.       this.editor.setKeymap((Keymap)null);
  343.       this.uninstallListeners(c);
  344.    }
  345.  
  346.    public int viewToModel(Point pt) {
  347.       if (this.painted) {
  348.          Rectangle alloc = this.getVisibleEditorRect();
  349.          return this.rootView.viewToModel((float)pt.x, (float)pt.y, alloc);
  350.       } else {
  351.          return -1;
  352.       }
  353.    }
  354. }
  355.